package com.ymatou.operate.trading.api.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.operate.trading.api.parameter.BuyerDelayReceiveTimeBean;
import com.ymatou.operate.trading.api.service.BuyerDelayReceiveTimeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 买家延长收货时间 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_BuyerDelayReceiveTime {

	private static BuyerDelayReceiveTimeBean buyerdelayreceivetimebean;
	private static BuyerDelayReceiveTimeCall buyerdelayreceivetimecall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买家延长收货时间");
	}

	@Before
	public void caseUp() {

		buyerdelayreceivetimebean = new BuyerDelayReceiveTimeBean();
		buyerdelayreceivetimecall = new BuyerDelayReceiveTimeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(buyerdelayreceivetimecall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BuyerDelayReceiveTime_001() {
		Logger.start(true,"买家延长收货时间成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");		
			PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
			int userId =placeOrderResp.getUserId();         
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);			
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);			
			// 商家发货
			new OrderCallService().dispatched(orderId, sellerId);
			Logger.comment("前置数据准备结束");
			// 参数
			buyerdelayreceivetimebean.setDelayDay(1);
			String delayTime = YMTDateUtil.getSysTime();
			buyerdelayreceivetimebean.setDelayTime(delayTime);
			buyerdelayreceivetimebean.setMemo("自动化测试买家延长时间");
			buyerdelayreceivetimebean.setOrderId(orderId);
			buyerdelayreceivetimebean.setUserId(userId);
			// 发送请求
			buyerdelayreceivetimecall.setData(buyerdelayreceivetimebean);
			buyerdelayreceivetimecall.callService();
			Logger.verifyEquals(ECode.SUCCESS, buyerdelayreceivetimecall.getCode(), "验证Code");
			// 数据库验证
			Thread.sleep(1000);
			DefaultWapper database = new DefaultWapper();
			Logger.verifyEquals(1, database.selectOrderFrozenByOrderId(orderId).size(), "验证延长收货记录");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BuyerDelayReceiveTime_002() {
		Logger.start(false,"订单不存在，买家延长收货时间失败");
		try {
			// 参数
			int orderId = 424;
			buyerdelayreceivetimebean.setDelayDay(1);
			String DelayTime = YMTDateUtil.getSysTime();
			buyerdelayreceivetimebean.setDelayTime(DelayTime);
			buyerdelayreceivetimebean.setMemo("自动化测试延长时间");
			buyerdelayreceivetimebean.setOrderId(orderId);
			buyerdelayreceivetimebean.setUserId(1113);
			// 发送请求
			buyerdelayreceivetimecall.setData(buyerdelayreceivetimebean);
			buyerdelayreceivetimecall.callService();
			Logger.verifyEquals(ECode.FAILED201, buyerdelayreceivetimecall.getCode(), "验证Code");
			Logger.verifyIsContains(ECode.FAILED201_MSG_11, buyerdelayreceivetimecall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BuyerDelayReceiveTime_003() {
		Logger.start(false,"此买家已经延长过收货时间");
		try {
			// 创建订单
			Logger.comment("前置数据准备");		
			PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
			int userId =placeOrderResp.getUserId();         
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);			
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);			
			// 商家发货
			new OrderCallService().dispatched(orderId, sellerId);
			Logger.comment("前置数据准备结束");
			// 参数
			buyerdelayreceivetimebean.setDelayDay(1);
			String delayTime = YMTDateUtil.getSysTime();
			buyerdelayreceivetimebean.setDelayTime(delayTime);
			buyerdelayreceivetimebean.setMemo("自动化测试延长时间");
			buyerdelayreceivetimebean.setOrderId(orderId);
			buyerdelayreceivetimebean.setUserId(userId);
			// 发送请求
			buyerdelayreceivetimecall.setData(buyerdelayreceivetimebean);
			buyerdelayreceivetimecall.callService();
			Logger.comment("重复请求");
			buyerdelayreceivetimecall.callService();
			Logger.verifyEquals(ECode.FAILED201, buyerdelayreceivetimecall.getCode(), "验证Code");
			Logger.verifyEquals(true, buyerdelayreceivetimecall.getMsg().contains(ECode.FAILED201_MSG_15), "验证Msg包含信息:"
					+ ECode.FAILED201_MSG_15);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BuyerDelayReceiveTime_004() {
		Logger.start(false,"买家不是订单所有者");
		try {
			// 创建订单
			Logger.comment("前置数据准备");		
			PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
			int userId =placeOrderResp.getUserId();         
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);			
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);			
			// 商家发货
			new OrderCallService().dispatched(orderId, sellerId);
			Logger.comment("前置数据准备结束");
			// 参数
			buyerdelayreceivetimebean.setDelayDay(1);
			String delayTime = YMTDateUtil.getSysTime();
			buyerdelayreceivetimebean.setDelayTime(delayTime);
			buyerdelayreceivetimebean.setMemo("自动化测试延长时间");
			buyerdelayreceivetimebean.setOrderId(orderId);
			buyerdelayreceivetimebean.setUserId(userId+1);
			// 发送请求
			buyerdelayreceivetimecall.setData(buyerdelayreceivetimebean);
			buyerdelayreceivetimecall.callService();
			Logger.verifyEquals(ECode.SUCCESS, buyerdelayreceivetimecall.getCode(), "验证Code");
			// 数据库验证
			Thread.sleep(1000);
			DefaultWapper database = new DefaultWapper();
			Logger.verifyEquals(1, database.selectOrderFrozenByOrderId(orderId).size(), "验证延长收货记录");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BuyerDelayReceiveTime_005() {
		Logger.start(false,"买家id为null");
		try {
			// 参数
			buyerdelayreceivetimebean.setDelayDay(1);
			String delayTime = YMTDateUtil.getSysTime();
			buyerdelayreceivetimebean.setDelayTime(delayTime);
			buyerdelayreceivetimebean.setMemo("自动化测试延长时间");
			buyerdelayreceivetimebean.setOrderId(245111);
			//buyerdelayreceivetimebean.setUserId(userId);
			// 发送请求
			buyerdelayreceivetimecall.setData(buyerdelayreceivetimebean);
			buyerdelayreceivetimecall.callService();
			Logger.verifyEquals(ECode.ERROR400, buyerdelayreceivetimecall.getCode(), "验证Code");
			Logger.verifyEquals(ECode.ERROR400_MSG_15,buyerdelayreceivetimecall.getMsg(),"验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BuyerDelayReceiveTime_006() {
		Logger.start(false,"延迟的天数为0");
		try {
			// 参数
			buyerdelayreceivetimebean.setDelayDay(0);
			String delayTime = YMTDateUtil.getSysTime();
			buyerdelayreceivetimebean.setDelayTime(delayTime);
			buyerdelayreceivetimebean.setMemo("自动化测试延长时间");
			buyerdelayreceivetimebean.setOrderId(132333);
			buyerdelayreceivetimebean.setUserId(1113);
			// 发送请求
			buyerdelayreceivetimecall.setData(buyerdelayreceivetimebean);
			buyerdelayreceivetimecall.callService();
			Logger.verifyEquals(ECode.ERROR400, buyerdelayreceivetimecall.getCode(), "验证Code");
			Logger.verifyEquals(ECode.ERROR400_MSG_16,buyerdelayreceivetimecall.getMsg(),"验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
    @Category(P3.class)
    @TestCase
    public void Tc_BuyerDelayReceiveTime_007() {
        Logger.start(false,"订单号为null");
        try {
            // 参数
            buyerdelayreceivetimebean.setDelayDay(1);
            String delayTime = YMTDateUtil.getSysTime();
            buyerdelayreceivetimebean.setDelayTime(delayTime);
            buyerdelayreceivetimebean.setMemo("自动化测试延长时间");
            //buyerdelayreceivetimebean.setOrderId(orderId);
            buyerdelayreceivetimebean.setUserId(1113);
            // 发送请求
            buyerdelayreceivetimecall.setData(buyerdelayreceivetimebean);
            buyerdelayreceivetimecall.callService();
            Logger.verifyEquals(ECode.ERROR400, buyerdelayreceivetimecall.getCode(), "验证Code");
            Logger.verifyEquals(ECode.ERROR400_MSG_13,buyerdelayreceivetimecall.getMsg(),"验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
	@Test
    @Category(P3.class)
    @TestCase
    public void Tc_BuyerDelayReceiveTime_008() {
        Logger.start(false,"订单还未发货,买家延迟收货失败");
        try {
            // 创建订单
            Logger.comment("前置数据准备");       
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            Logger.comment("前置数据准备结束");
            // 参数
            buyerdelayreceivetimebean.setDelayDay(2);
            String delayTime = YMTDateUtil.getSysTime();
            buyerdelayreceivetimebean.setDelayTime(delayTime);
            buyerdelayreceivetimebean.setMemo("自动化测试延长时间");
            buyerdelayreceivetimebean.setOrderId(orderId);
            buyerdelayreceivetimebean.setUserId(userId);
            // 发送请求
            buyerdelayreceivetimecall.setData(buyerdelayreceivetimebean);
            buyerdelayreceivetimecall.callService();
            Logger.verifyEquals(ECode.FAILED201, buyerdelayreceivetimecall.getCode(), "验证Code");
            Logger.verifyEquals(ECode.FAILED201_MSG_64,buyerdelayreceivetimecall.getMsg(),"验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
}